package com.allen.mk.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.allen.mk.admin.entity.SysRole;
import com.allen.mk.admin.mapper.SysRoleMapper;
import com.allen.mk.admin.service.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import page.CommonPage;
import request.PageParamRequest;
import request.SysRoleRequest;
import response.SysRoleResponse;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Allen
 * @since 2024-04-11
 */
@Slf4j
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    public PageInfo<SysRoleResponse> getList(PageParamRequest pageParamRequest){
        //构造一个页对象
        Page<SysRole> rolePage = PageHelper.startPage(pageParamRequest.getPage(),pageParamRequest.getLimit());
        //组装条件，因为该方法直接返回list，所以不需要条件
        LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = Wrappers.lambdaQuery();
//        sysRoleLambdaQueryWrapper.orderByDesc(SysRole::getCreateTime);
//        sysRoleLambdaQueryWrapper.eq(SysRole::getRoleName,false);
        List<SysRole> roles = sysRoleMapper.selectList(sysRoleLambdaQueryWrapper);
        log.info(roles.toString());
        if (CollUtil.isEmpty(roles)){
            return CommonPage.copyPageInfo(rolePage,CollUtil.newArrayList());
        }

        //将查到的列表数据重新组装成response对象列表
        List<SysRoleResponse> roleResponses = roles.stream().map(e -> {
            SysRoleResponse sysRoleResponse = new SysRoleResponse();
            BeanUtils.copyProperties(e,sysRoleResponse);
            return sysRoleResponse;
        }).collect(Collectors.toList());
        return CommonPage.copyPageInfo(rolePage,roleResponses);
    }

    public Boolean saveRole(SysRoleRequest sysRoleRequest){
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleRequest,sysRole);
        LocalDateTime now = LocalDateTime.now();
        sysRole.setCreateTime(now);
        Integer flag = sysRoleMapper.insert(sysRole);
        if (flag == 1){
            return true;
        }
        return false;
    }

    public Boolean deleteRole(Integer id){
        Integer flag = sysRoleMapper.deleteById(id);
        if (flag == 1){
            return true;
        }
        return false;
    }

    public SysRoleResponse selectRole(String rolename){
        LambdaQueryWrapper<SysRole> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(SysRole::getRoleName,rolename);
        SysRole sysRole = sysRoleMapper.selectOne(queryWrapper);
        SysRoleResponse sysRoleResponse = new SysRoleResponse();
        BeanUtils.copyProperties(sysRole,sysRoleResponse);
        return sysRoleResponse;
    }


    public Boolean updateRole(SysRoleRequest request) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(request,sysRole);
        Integer flag = sysRoleMapper.updateById(sysRole);
        if(flag == 1){
            return true;
        }
        return false;
    }
}
